package com.flex.mobile.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.stereotype.Service;

import com.flex.mobile.dao.IGenericDAO;
import com.flex.mobile.dao.model.UserStatus;

@Service("userStatusDao")
public class UserStatusDao implements IGenericDAO<UserStatus> {
	private final SimpleJdbcTemplate template;

	private final SimpleJdbcInsert insertContact;

	@Autowired
	public UserStatusDao(DataSource dataSource) {
		this.template = new SimpleJdbcTemplate(dataSource);
		this.insertContact = new SimpleJdbcInsert(dataSource).withTableName("USER_STATUS").usingGeneratedKeyColumns("ID");
	}

	private final RowMapper<UserStatus> summaryRowMapper = new ParameterizedRowMapper<UserStatus>() {

		@Override
		public UserStatus mapRow(ResultSet rs, int rowNum) throws SQLException {
			UserStatus userStatus = new UserStatus();
			userStatus.setId(rs.getLong("ID"));
			userStatus.setTid(rs.getLong("T_ID"));
			userStatus.setCurrentStatus(rs.getInt("CURRENT_STATUS"));
			userStatus.setLatitude(rs.getDouble("LATITUDE"));
			userStatus.setLongitude(rs.getDouble("LONGITUDE"));
			userStatus.setOrderId(rs.getLong("ORDER_ID"));
			userStatus.setUserType(rs.getInt("USER_TYPE"));
			userStatus.setUpdateDate(rs.getDate("UPDATE_DATE"));
			userStatus.setLoginTimes(rs.getInt("LOGIN_TIMES"));
			return userStatus;
		}

	};

	@Override
	public UserStatus create(UserStatus userStatus) {
		Map<String, Object> parameters = new HashMap<String, Object>();
		parameters.put("ID", null);
		//userStatus需要在传入的时候设置cid
		parameters.put("T_ID", userStatus.getTid());
		parameters.put("CURRENT_STATUS", userStatus.getCurrentStatus());
		parameters.put("LATITUDE", userStatus.getLatitude());
		parameters.put("LONGITUDE", userStatus.getLongitude());
		parameters.put("ORDER_ID", userStatus.getOrderId());
		parameters.put("USER_TYPE", userStatus.getUserType());
		parameters.put("UPDATE_DATE", userStatus.getUpdateDate());
		parameters.put("LOGIN_TIMES", userStatus.getLoginTimes());
		Number id = this.insertContact.executeAndReturnKey(parameters);
		userStatus.setId(id.longValue());
		return userStatus;
	}

	@Override
	public List<UserStatus> findAll() {
		return this.template.query("SELECT * FROM USER_STATUS ", this.summaryRowMapper);
	}
	/**
	 * 根据用户类型查，0：cust 1：driver
	 * @param type
	 * @return
	 */
	public List<UserStatus> findAllByType(int type) {
		return this.template.query("SELECT * FROM USER_STATUS where user_type=?", this.summaryRowMapper,type);
	}
	
	public List<UserStatus>findOnlineByType(int type){
		return this.template.query("SELECT * FROM USER_STATUS where current_status=1 and user_type=? ", this.summaryRowMapper,type);

	}
	
	@Override
	public UserStatus findById(long id) {
		String sql = "select * from USER_STATUS where id =?";
		return this.template.queryForObject(sql, summaryRowMapper, id);
	}
	
	public UserStatus findByTId(long id) {
		String sql = "select * from USER_STATUS where T_ID =?";
		List<UserStatus> status = this.template.query(sql, summaryRowMapper, id);
		return status.size() > 0? status.get(0) : null;
	}
	
	@Override
	public List<UserStatus> findByName(String name) {
		// String sql = "select * from USER_STATUS e " +
		// "where e.NAME  like ?";
		// return this.template.query(sql, summaryRowMapper,
		// "%"+name.trim()+"%");
		return null;
	}

	@Override
	public boolean remove(UserStatus userStatus) {
		int count = this.template.update("DELETE FROM USER_STATUS WHERE id=?", userStatus.getId());
		return count == 1;
	}

	@Override
	public boolean update(UserStatus userStatus) {
		StringBuffer sql = new StringBuffer();
		sql.append("UPDATE USER_STATUS SET CURRENT_STATUS=?, LATITUDE=?, LONGITUDE=?, ORDER_ID=?, USER_TYPE=? ,UPDATE_DATE=?,LOGIN_TIMES=? ");
		sql.append(" WHERE id=?");
		int count = this.template.update(sql.toString(), userStatus.getCurrentStatus(), userStatus.getLatitude(), userStatus.getLongitude(), userStatus.getOrderId(), userStatus.getUserType(),
				userStatus.getUpdateDate(), userStatus.getLoginTimes(), userStatus.getId());
		return count == 1;
	}
	
	public boolean updateStatusByTid(int status, long tid) {
	   StringBuffer sql = new StringBuffer();
      sql.append("UPDATE USER_STATUS SET CURRENT_STATUS=? WHERE T_ID=?");
      int count = this.template.update(sql.toString(), status, tid);
      return count == 1;
	}
	
}
